package figure8.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import java.io.IOException;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDotRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;
import org.jfugue.MidiParser;

import figure8.DrumMachineTrack;
import figure8.WAVTrack;
import figure8.events.TrackChangeListener;
import figure8.events.TrackChangedEvent;

public class DrumMachineGraph extends JPanel implements TrackChangeListener {

	XYSeries series1;
	XYSeries series2;
	XYSeries series3;
	XYSeries series4;
	XYSeries series5;
	XYSeries series6;
	XYSeries series7;
	XYSeries series8;
	XYSeries series9;
	XYSeries series10;
	JFreeChart chart;
	XYSeriesCollection dataSet;
	DrumMachineTrack drumTrack;
	Boolean firstTime;
	
	public DrumMachineGraph(DrumMachineTrack track) {
		drumTrack = track;
		firstTime = true;
		//Only makes one when you start the program at the moment. It doesnt update when you change beats yet.
		this.setLayout(new BorderLayout());
		dataSet = new XYSeriesCollection();
		series1 = new XYSeries("1");
		series2 = new XYSeries("2");
		series3 = new XYSeries("3");
		series4 = new XYSeries("4");
		series5 = new XYSeries("5");
		series6 = new XYSeries("6");
		series7 = new XYSeries("7");
		series8 = new XYSeries("8");
		series9 = new XYSeries("9");
		series10 = new XYSeries("10");
		
		capture(track.getDrumMachine(), track.getNumberBeats());
		
		chart = ChartFactory.createScatterPlot(null,null,null,dataSet,PlotOrientation.VERTICAL,false,true,false);
		ChartPanel chPanel = new ChartPanel(chart);
		
		chart.setPadding(new RectangleInsets(0, 0, 0, 0));
		
		XYPlot plot = chart.getXYPlot();
		ValueAxis axis = plot.getDomainAxis();
		axis.setRange(-1, track.getNumberBeats());
		plot.getRangeAxis().setRange(-1,11);
		plot.getRangeAxis().setVisible(false);
		plot.getDomainAxis().setVisible(false);
		chart.setBackgroundPaint(Color.WHITE);
		XYDotRenderer renderer = new XYDotRenderer(); 
		renderer.setSeriesPaint(0, Color.magenta); 
		renderer.setSeriesPaint(1, Color.blue); 
		renderer.setSeriesPaint(2, Color.black); 
		renderer.setSeriesPaint(3, Color.orange);
		renderer.setSeriesPaint(4, Color.pink); 
		renderer.setSeriesPaint(5, Color.yellow); 
		renderer.setSeriesPaint(6, Color.gray); 
		renderer.setSeriesPaint(7, Color.green); 
		renderer.setSeriesPaint(8, Color.cyan); 
		renderer.setSeriesPaint(9, Color.red); 
		renderer.setDotWidth(10);
		renderer.setDotHeight(10);
		plot.setRenderer(renderer);
		
		this.add(chPanel, BorderLayout.CENTER);
		
		track.addChangeListener(this);
	}
	
	public void capture(Boolean[][] drum, int beats){
		int i = 0;
		int j;
		series1.clear();
		series2.clear();
		series3.clear();
		series4.clear();
		series5.clear();
		series6.clear();
		series7.clear();
		series8.clear();
		series9.clear();
		series10.clear();
		while (i < 10) {
			j = 0;
			while (j < beats) {
				if(drum[j][i] == true) {
					switch (i) {
						case 0: series1.addOrUpdate((Integer) j, (Integer) (10-i)); break;
						case 1: series2.addOrUpdate((Integer) j, (Integer) (10-i)); break;
						case 2: series3.addOrUpdate((Integer) j, (Integer) (10-i)); break;
						case 3: series4.addOrUpdate((Integer) j, (Integer) (10-i)); break;
						case 4: series5.addOrUpdate((Integer) j, (Integer) (10-i)); break;
						case 5: series6.addOrUpdate((Integer) j, (Integer) (10-i)); break;
						case 6: series7.addOrUpdate((Integer) j, (Integer) (10-i)); break;
						case 7: series8.addOrUpdate((Integer) j, (Integer) (10-i)); break;
						case 8: series9.addOrUpdate((Integer) j, (Integer) (10-i)); break;
						case 9: series10.addOrUpdate((Integer) j, (Integer) (10-i)); break;
					}
				}
				j++;
			}
			i++;
		}
		if(firstTime == true) {
			dataSet.addSeries(series1);
			dataSet.addSeries(series2);
			dataSet.addSeries(series3);
			dataSet.addSeries(series4);
			dataSet.addSeries(series5);
			dataSet.addSeries(series6);
			dataSet.addSeries(series7);
			dataSet.addSeries(series8);
			dataSet.addSeries(series9);
			dataSet.addSeries(series10);
		}
		if(firstTime == true) firstTime = false;
	}
	
	public void changeGraph() {
		int i = 0;
		int j = 0;
		Boolean[][] dm = drumTrack.getDrumMachine();
		// recreate graph
		capture(drumTrack.getDrumMachine(), drumTrack.getNumberBeats());
	}

	@Override
	public void trackChanged(TrackChangedEvent event) {
		this.changeGraph();
	}

}